#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int o[N];
vector<int> a[N];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{

		int n, m;
		scanf("%d %d", &n, &m);
		for (int i = 1; i <= n; i++)
		{
			a[i].push_back(0);
			for (int j = 1; j <= m; j++)
			{
				int s = 0;
				scanf("%d", &s);
				a[i].push_back(s);
			}
		}

		int sum = 0;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				if (a[i][j] == 1)
				{
					o[i]++;
					sum++;
				}
			}
		}
		if (sum % n != 0)
		{
			cout << -1 << endl;
			for (int i = 1; i <= n; i++)
			{
				a[i].clear();
				o[i]=0;
			}
			continue;
		}

		int ave = sum / n;
		int ans = 0;
		for (int i = 1; i <= n; i++)
		{
			if (o[i] > ave)
			{
				ans += o[i] - ave;
			}
		}
		cout << ans << endl;
		for (int j = 1; j <= m; j++)
		{
			vector<int> y;
			for (int i = 1; i <= n; i++)
			{
				if (a[i][j] == 1 && o[i] > ave)
				{
					y.push_back(i);
				}
			}
			int idx = 0;
			for (int i = 1; i <= n; i++)
			{
				if (a[i][j] == 0 && o[i] < ave && y.size()>0)
				{
					cout << y[idx] << ' ' << i << ' ' << j << endl;
					o[y[idx]]--;
					o[i]++;
					idx++;
					if (idx == y.size())
					{
						break;
					}

				}
			}
		}
		for (int i = 1; i <= n; i++)
		{
			a[i].clear();
			o[i] = 0;
		}
		1;
	}

	return 0;
}

